<?php
/*----------------------------------------------------------------------
 * 前台首页
 * Author: ming.nie
----------------------------------------------------------------------*/

namespace Home\Controller;
use Think\Controller;
class DataSyncController extends Controller
{
	/*影院会员新增，更新同步接口
	 *接收数据JSON格式
	*/
	public function syncMember(){
		//模拟数据
		$host_name = $_POST['host_name'];
		$post_data = $_POST['data'];

		if(!empty($post_data) && !empty($host_name)){
			$data = json_decode($post_data,true);
			foreach ($data as $v){
			//如果没有该条数据 进行新增 如果没有则更新
				$map['id'] = $v['id'];
				$info = M('member')->where($map)->find();
				//构建数据
					$abc['id'] = $v['id'];
					$abc['host_name' ]= $host_name;
					$abc['sex' ]= $v['sex'];
					$abc['birthday' ]= $v['birthday'];
					$abc['name'] = $v['name'];
					$abc['phone_number'] = $v['phone_number'];
					$abc['out_total_amount'] = $v['out_total_amount'];
					$abc['ctime' ]= $v['ctime'];
				if($info){
					$result = M('member')->where($map)->save($abc); // 根据条件更新记录
				}else{
					$result = M('member')->data($abc)->add(); // 根据条件更新记录
				}
			}
			echo 'success';
			
		}
	}

	/*同步网络申请数据
	 *
	*/
	public function syncApply(){
		$url='http://e.ftxjoy.com/getdata.php';  
		$html = json_decode(file_get_contents($url),true); 
		foreach($html as $k => $v){
			$data['id'] = $v['id'];
			$data['username'] = $v['username'];
			$data['phone'] = $v['phone'];
			$data['email'] = $v['email'];
			$data['province'] = $v['city'];
			$data['district'] = $v['district'];
			$data['address'] = $v['address'];
			$data['ctime'] = $v['ctime'];
			$data['IP'] = $v['IP'];
			$data['send'] = $v['send'];
			$data['remark'] = $v['remark'];
			//判断ID是否存在。
			$map['id'] = $v['id'];
			$result = M('apply')->where($map)->find();
			if(!$result){
				M('apply')->data($data)->add();	
				echo "同步编号：".$data['id']." 成功"."<BR>";
			}
		}
	}

	
	/*更新代理商信息接口
	 *
	*/
	public function syncAgent(){
		$params = I('request.');
		// $params['name'] = 211111;
		// $params['level'] = 33333; 
		// $params = Array(
			// 'agentid' => 'dl201592812176',
			// 'level' => 1,
			// 'contractnum' => 23423,
			// 'name' => 423,
			// 'starttime' => 1441641600,
			// 'endtime' => 1442419200,
			// 'url_file' => '',
			// 'area1' => '4',
			// 'area2' => 72,
			// 'area3' => '',
			// 'address' => '问问',
			// 'companyname' => '非服务',
			// 'companyuser' => '认为二位',
			// 'card' => 123456787678987,
			// 'pic' => 'a:3:{i:0;s:0:"";i:1;s:0:"";i:2;s:0:"";}',
			// 'flag' => 0,
			// 'status' => 1,
			// 'ctime' => 1443412176,
			// 'user_id' => 2,
			// 'phone' => 13456787656,
			// 'email' => 'qq@163.com',
			// 'username' => 3123,
			// 'qq' => 3333333,
			// 'content' => '非我方',
			// 'uptime' => 1443412176,
		// );
		if($params){
			$map['agentid'] = $params['agentid'];
			$isInfo = M('agent')->where($map)->find(); //用于判断是否存在该条信息
			//由于表字段名称不一致，需要逐个判断
			if(!empty($params['name'])){
				$data['agent_name'] = $params['name'];  //代理商名称
			}
			if(!empty($params['level'])){
				$data['agent_level'] = $params['level'];  //代理商等级
			}
			if(!empty($params['user_id'])){
				$data['agent_user_id'] = $params['user_id'];  //代理商管理员ID
			}
			if(!empty($params['contractnum'])){
				$data['agent_contract_no'] = $params['contractnum'];  //代理商合同号
			}
			if(!empty($params['starttime'])){
				$data['agent_starttime'] = $params['starttime'];  //合同开始时间
			}
			if(!empty($params['endtime'])){
				$data['agent_endtime'] = $params['endtime'];  //合同结束时间
			}
			if(!empty($params['flag'])){
				$data['agent_flag'] = $params['flag'];  //是否续约：1已续约,2未已续约
			}
			if(!empty($params['url_file'])){
				$data['agent_contract'] = $params['url_file'];  //合同路径
			}
			if(!empty($params['phone'])){
				$data['agent_tel'] = $params['phone'];  //联系电话
			}
			if(!empty($params['area1'])){
				$data['area_1'] = $params['area1'];  //地区1
			}
			if(!empty($params['area2'])){
				$data['area_2'] = $params['area2'];  //地区2
			}
			if(!empty($params['area3'])){
				$data['area_3'] = $params['area3'];  //地区3
			}
			if(!empty($params['address'])){
				$data['address'] = $params['address'];  //地址
			}
			if(!empty($params['username'])){
				$data['agent_contacts'] = $params['username'];  //联系人
			}
			if(!empty($params['email'])){
				$data['agent_email'] = $params['email'];  //email
			}			
			if(!empty($params['qq'])){
				$data['agent_qq'] = $params['qq'];  //QQ
			}
			if(!empty($params['companyname'])){
				$data['agent_company_name'] = $params['companyname'];  //公司名
			}
			if(!empty($params['companyuser'])){
				$data['agent_legal_person'] = $params['companyuser'];  //法人
			}
			if(!empty($params['card'])){
				$data['agent_identity'] = $params['card'];  //身份证号
			}
			if(!empty($params['pic'])){
				$data['agent_honor'] = $params['pic'];  //资质图片
			}
			if(!empty($params['content'])){
				$data['agent_mark'] = $params['content'];  //备注
			}
			if(!empty($params['status'])){
				$data['agent_status'] = $params['status'];  //状态
			}
			if(!empty($params['ctime'])){
				$data['agent_time'] = $params['ctime'];  //创建时间
			}
			if(!empty($params['uptime'])){
				$data['uptime'] = $params['uptime'];  //更新时间
			}
			//同步数据，写入数据库
			if(!$isInfo){ //如果不存在 ， 新建信息
				$data['agentid'] = $params['agentid'];  //代理商编号
				$result = M('agent')->where($map)->add($data);
				if($result !== false){
					$msg['status'] = 100;
					$msg['info'] = '同步创建数据成功';
				}				
			}else{//如果存在 ，更新信息
				$result = M('agent')->where($map)->save($data);
				if($result !== false){
					$msg['status'] = 200;
					$msg['info'] = '同步更新数据成功';
				}
			}
		}else{
			$msg['status'] = 300;
			$msg['info'] = '参数错误';
		}
		$this->ajaxReturn($msg);
	}
	
	/*同步代理商数据*/
	public function syncSales(){
		$arr = $_POST['data'];
		$list = json_decode($arr,true);
		foreach ($list as $params){
			$map['agentid'] = $params['agentid'];
			$isInfo = M('agent')->where($map)->find(); //用于判断是否存在该条信息	
			
			if(!empty($params['name'])){
				$data['agent_name'] = $params['name'];  //代理商名称
			}
			if(!empty($params['level'])){
				$data['agent_level'] = $params['level'];  //代理商等级
			}
			if(!empty($params['user_id'])){
				$data['agent_user_id'] = $params['user_id'];  //代理商管理员ID
			}
			if(!empty($params['contractnum'])){
				$data['agent_contract_no'] = $params['contractnum'];  //代理商合同号
			}
			if(!empty($params['starttime'])){
				$data['agent_starttime'] = $params['starttime'];  //合同开始时间
			}
			if(!empty($params['endtime'])){
				$data['agent_endtime'] = $params['endtime'];  //合同结束时间
			}
			if(!empty($params['flag'])){
				$data['agent_flag'] = $params['flag'];  //是否续约：1已续约,2未已续约
			}
			if(!empty($params['url_file'])){
				$data['agent_contract'] = $params['url_file'];  //合同路径
			}
			if(!empty($params['phone'])){
				$data['agent_tel'] = $params['phone'];  //联系电话
			}
			if(!empty($params['area1'])){
				$data['area_1'] = $params['area1'];  //地区1
			}
			if(!empty($params['area2'])){
				$data['area_2'] = $params['area2'];  //地区2
			}
			if(!empty($params['area3'])){
				$data['area_3'] = $params['area3'];  //地区3
			}
			if(!empty($params['address'])){
				$data['address'] = $params['address'];  //地址
			}
			if(!empty($params['username'])){
				$data['agent_contacts'] = $params['username'];  //联系人
			}
			if(!empty($params['email'])){
				$data['agent_email'] = $params['email'];  //email
			}			
			if(!empty($params['qq'])){
				$data['agent_qq'] = $params['qq'];  //QQ
			}
			if(!empty($params['companyname'])){
				$data['agent_company_name'] = $params['companyname'];  //公司名
			}
			if(!empty($params['companyuser'])){
				$data['agent_legal_person'] = $params['companyuser'];  //法人
			}
			if(!empty($params['card'])){
				$data['agent_identity'] = $params['card'];  //身份证号
			}
			if(!empty($params['pic'])){
				$data['agent_honor'] = $params['pic'];  //资质图片
			}
			if(!empty($params['content'])){
				$data['agent_mark'] = $params['content'];  //备注
			}
			if(!empty($params['status'])){
				$data['agent_status'] = $params['status'];  //状态
			}
			if(!empty($params['ctime'])){
				$data['agent_time'] = $params['ctime'];  //创建时间
			}
			if(!empty($params['uptime'])){
				$data['uptime'] = $params['uptime'];  //更新时间
			}
			//同步数据，写入数据库
			if(!$isInfo){ //如果不存在 ， 新建信息
				$data['agentid'] = $params['agentid'];  //代理商编号
				M('agent')->add($data);			
			}else{//如果存在 ，更新信息
				M('agent')->where($map)->save($data);
			}	
		}
		echo 'success';
	}
	
	
}